﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using PopupControl;
using SharpDiary.Core.Utility;
using SharpDiary.Core.ORMapping;

namespace SharpDiary.Core.Controls
{
    public partial class EmotionEditor : UserControl
    {
        private DialogResult _result = DialogResult.Cancel;

        public DialogResult Result
        {
            get { return _result; }
            set { _result = value; }
        }

        private EmotionInfo _emotion;

        public EmotionInfo Emotion
        {
            get { return _emotion; }
            set
            {
                _emotion = value;
                ShowEmotion(value);
            }
        }

        private bool _isModified = false;

        public bool IsModified
        {
            get { return _isModified; }
            set
            {
                _isModified = value;
                OnModifiedChanged(this, EventArgs.Empty);
            }
        }

        public event EventHandler<EventArgs> ModifiedChanged;

        protected virtual void OnModifiedChanged(object sender, EventArgs e)
        {
            if (ModifiedChanged != null)
            {
                ModifiedChanged(sender, e);
            }
        }

        public EmotionEditor()
        {
            InitializeComponent();

            DoubleBuffered = true;
            ResizeRedraw = true;

            /*
            if (!DesignMode)
            {
                ShowEmotions();
            }
            */
        }

        /// <summary>
        /// 显示心情信息。
        /// </summary>
        private void ShowEmotion(EmotionInfo emotion)
        {
            if (emotion == null || string.IsNullOrEmpty(emotion.Name))
            {
                pictureBoxEmotion.Image = null;
                lblEmotion.Text = "未知心情";
                lblDescription.Text = string.Empty;

                return;
            }

            pictureBoxEmotion.Image = Utils.BytesToImage(emotion.Picture);
            lblDescription.Text = emotion.Description;
            lblEmotion.Text = emotion.Name;
        }

        /// <summary>
        /// 选择了心情图示，点击确定后触发此事件。
        /// </summary>
        public event EventHandler<EventArgs> EmotionChanged;

        protected virtual void OnEmotionChanged(object sender, EventArgs e)
        {
            if (EmotionChanged != null) { EmotionChanged(sender, e); }
        }

        /// <summary>
        /// 显示可选心情图示。
        /// </summary>
        public void ShowEmotions()
        {
            imageList.Images.Clear();
            imageListBoxEmotion.Items.Clear();

            List<EmotionInfo> emotions = EmotionInfo.GetAllEmotions();
            for (int i = 0; i < emotions.Count; i++)
            {
                EmotionInfo e = emotions[i];
                imageList.Images.Add(Utils.BytesToImage(e.Picture));

                ImageListBoxItem newItem = new ImageListBoxItem(e.Name, i);
                newItem.Tag = e;
                imageListBoxEmotion.Items.Add(newItem);
            }
        }

        private void btnDone_Click(object sender, EventArgs e)
        {
            if (imageListBoxEmotion.SelectedItem != null)
            {
                _emotion = (imageListBoxEmotion.SelectedItem as ImageListBoxItem).Tag as EmotionInfo;
            }

            Result = DialogResult.OK;
            (Parent as Popup).Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            ShowEmotion(_emotion);

            Result = DialogResult.Cancel;
            (Parent as Popup).Close();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            UserSettings.ShellInstance.ExecuteAction("SharpDiary.Emotion", "c57f2629-3e7d-422e-bc9a-4bf9118f193f");
        }

        private void lblManageEmotions_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            UserSettings.ShellInstance.ExecuteAction("SharpDiary.Emotion", "c57f2629-3e7d-422e-bc9a-4bf9118f193f");
        }

        private void imageListBoxEmotion_SelectedIndexChanged(object sender, EventArgs e)
        {
            ImageListBoxItem item = imageListBoxEmotion.SelectedItem as ImageListBoxItem;
            if (item != null && item.Tag != null)
            {
                EmotionInfo emotion = item.Tag as EmotionInfo;
                ShowEmotion(emotion);

                if (emotion != _emotion)
                {
                    if (emotion != null && _emotion != null && (emotion.Id != _emotion.Id))
                    {
                        IsModified = true;
                    }
                }
            }
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            base.OnPaintBackground(e);

            Rectangle rect = this.ClientRectangle;
            rect.Width -= 1;
            rect.Height -= 1;

            ControlPaint.DrawVisualStyleBorder(e.Graphics, rect);
        }

        private void imageListBoxEmotion_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            btnDone.PerformClick();
        }
    }
}
